<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 翻译API配置模型
 */
class TranslateApi extends Model
{
    protected $table = 'translate_apis';

    protected function getOptions(): array
    {
        return [
            'type'               => [
                'config' => 'json',
            ],
            'autoWriteTimestamp' => true,
            'jsonAssoc'          => true,
        ];
    }

    /**
     * 获取启用的API列表
     */
    public static function getEnabledApis()
    {
        return self::where('is_enabled', 1)
            ->order('priority', 'asc')
            ->select();
    }

    /**
     * 获取可用的API（未超过每日限制）
     */
    public static function getAvailableApi()
    {
        return self::where('is_enabled', 1)
            ->where('daily_used', '<', 'daily_limit')
            ->order('priority', 'asc')
            ->find();
    }

    /**
     * 增加API使用次数
     */
    public function incrementUsage($count = 1)
    {
        return $this->save([
            'daily_used' => $this->daily_used + $count
        ]);
    }

    /**
     * 重置每日使用次数
     */
    public static function resetDailyUsage()
    {
        return self::where('is_enabled', 1)->update([
            'daily_used' => 0
        ]);
    }

    /**
     * 检查API是否可用
     */
    public function isAvailable()
    {
        return $this->is_enabled &&
            ($this->daily_limit == 0 || $this->daily_used < $this->daily_limit);
    }

    /**
     * 获取API配置
     */
    public function getApiConfig()
    {
        return [
            'provider'   => $this->provider,
            'api_key'    => $this->api_key,
            'api_secret' => $this->api_secret,
            'endpoint'   => $this->endpoint,
            'config'     => $this->config ?: []
        ];
    }

    /**
     * 验证API配置
     */
    public function validateConfig()
    {
        $required = ['api_key', 'endpoint'];

        foreach ($required as $field) {
            if (empty($this->$field)) {
                return false;
            }
        }

        // 特定提供商的额外验证
        switch ($this->provider) {
            case 'baidu':
                return !empty($this->api_secret);
            case 'google':
                return !empty($this->api_key);
            case 'deepl':
                return !empty($this->api_key);
            case 'openai':
                return !empty($this->api_key);
            default:
                return true;
        }
    }

    /**
     * 获取提供商选项
     */
    public static function getProviderOptions()
    {
        return [
            'baidu'   => '百度翻译',
            'google'  => '谷歌翻译',
            'deepl'   => 'DeepL翻译',
            'openai'  => 'OpenAI翻译',
            'youdao'  => '有道翻译',
            'tencent' => '腾讯翻译',
        ];
    }

    /**
     * 获取提供商名称
     */
    public function getProviderNameAttr()
    {
        $providers = self::getProviderOptions();
        return $providers[$this->provider] ?? $this->provider;
    }
}
